package com.ecole.mines.emargement;

import java.util.ArrayList;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ecole.mines.model.Cours;
import com.ecole.mines.model.Promotion;
import com.ecole.mines.utils.ListCoursAdapter;
import com.ecole.mines.utils.ListPromoAdapter;

public class FragmentListePromoGroupe extends Fragment implements
		OnItemClickListener {

	private ListView listViewPromotion, listViewCours;
	private TextView messageInfo, messageInfoCours;
	private Promotion promoEnCours;

	public FragmentListePromoGroupe() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_main, container,
				false);

		listViewPromotion = (ListView) rootView.findViewById(R.id.listPromo);
		listViewPromotion.setOnItemClickListener(this);

		listViewCours = (ListView) rootView.findViewById(R.id.listCours);
		listViewCours.setOnItemClickListener(this);

		this.messageInfo = (TextView) rootView
				.findViewById(R.id.messageInfoPromo);
		this.messageInfoCours = (TextView) rootView
				.findViewById(R.id.messageInfoCours);

		return rootView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (parent == this.listViewPromotion) {
			Promotion p = (Promotion) this.listViewPromotion
					.getItemAtPosition(position);
			this.majListCours(p.getCours());
			this.promoEnCours = p;
		} else if (parent == this.listViewCours) {
			Cours c = (Cours) this.listViewCours.getItemAtPosition(position);
			((MainActivity) getActivity()).majDetailCours(this.promoEnCours, c);
		}
	}

	public void majListePromo(ArrayList<Promotion> listPromotion) {
		if (listPromotion != null && listPromotion.size() != 0) {
			// Création et initialisation de l'Adapter pour les promotions
			ListPromoAdapter adapter = new ListPromoAdapter(getActivity(),
					R.layout.activity_main, listPromotion);

			// Initialisation de la liste avec les donn�es
			this.listViewPromotion.setAdapter(adapter);
			this.listViewPromotion.setVisibility(View.VISIBLE);
			this.messageInfo.setVisibility(View.INVISIBLE);

		} else {
			this.messageInfo.setVisibility(View.VISIBLE);
			this.listViewPromotion.setVisibility(View.INVISIBLE);
			this.messageInfo.setText("Aucune promotion trouvée");
		}
	}

	public void majListCours(ArrayList<Cours> listCours) {
		if (listCours != null && listCours.size() != 0) {
			// Création et initialisation de l'Adapter pour les cours
			ListCoursAdapter adapter = new ListCoursAdapter(getActivity(),
					R.layout.activity_main, listCours);

			// Initialisation de la liste avec les donn�es
			listViewCours.setAdapter(adapter);
			listViewCours.setVisibility(View.VISIBLE);
			this.messageInfoCours.setVisibility(View.INVISIBLE);
		} else {
			this.messageInfo.setText("Aucun cours trouvé");
			this.messageInfoCours.setVisibility(View.VISIBLE);
			this.listViewCours.setVisibility(View.INVISIBLE);
			Toast.makeText(getActivity(), "Aucun cours trouvé",
					Toast.LENGTH_SHORT).show();
		}
	}

	public void cacherListeCours() {
		this.listViewCours.setVisibility(View.INVISIBLE);

	}
}
